/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rafkind.resrev.web.page.component;

import com.rafkind.resrev.dao.ReservationDao;
import com.rafkind.resrev.data.User;
import com.rafkind.resrev.web.Session;
import com.rafkind.resrev.web.page.CalendarPage;
import com.rafkind.resrev.web.page.EditUser;
import com.rafkind.resrev.web.page.ReservationsPage;
import com.rafkind.resrev.web.page.ResourcesPage;
import com.rafkind.resrev.web.page.UsersPage;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Fictional Hero
 */
public class TitleMenu extends Panel {
  
  @SpringBean
  private ReservationDao reservationDao;

  public TitleMenu(String id) {
    super(id);
    
    final User user = ((Session)getSession()).getCurrentUser();
    
    add(new Label("title", new PropertyModel(getApplication(), "title")));
    
    add(new Link("calendarLink"){
      @Override
      public void onClick() {
        setResponsePage(CalendarPage.class);
      }      
    });
    
    WebMarkupContainer userContainer = new WebMarkupContainer("userContainer");
    userContainer.add(new Link("userLink"){
      @Override
      public void onClick() {
        setResponsePage(UsersPage.class);
      }
    });    
    userContainer.setVisible(((Session)getSession()).getCurrentUser().isAdmin());
    
    add(userContainer);
    
    WebMarkupContainer resourceContainer = new WebMarkupContainer("resourceContainer");
    resourceContainer.add(new Link("resourceLink"){
      @Override
      public void onClick() {
        setResponsePage(ResourcesPage.class);
      }
    });
    resourceContainer.setVisible(user.isAdmin());
    
    add(resourceContainer);
    
    Link reservationsLink = new Link("reservationLink"){
      @Override
      public void onClick() {
        setResponsePage(ReservationsPage.class);
      }
    };
    
    int count = reservationDao.countApprovableReservations(user);
    
    reservationsLink.add(new Label("needAttention", 
        (count == 0 ? "" :
          count == 1 ? "(1 reservation needs attention)" 
            : "(" + count + " reservations need attention)")));
    
    add(reservationsLink);
    
    
    Link editCurrentUserLink = new Link("editCurrentUserLink"){
      @Override
      public void onClick() {
        setResponsePage(new EditUser(new Model(user), getPage()));
      }
    };
    editCurrentUserLink.add(new Label("loggedInUserName", 
        new PropertyModel(user, "name")));
    add(editCurrentUserLink);
    
    add(new Link("logoutLink"){

      @Override
      public void onClick() {
        getSession().invalidate();
        
        getRequestCycle().setResponsePage(CalendarPage.class);
        
        getRequestCycle().setRedirect(true);
      }      
    });    
  }
}
